﻿using IService.Blog;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace BlogAPI.Controllers
{
    /// <summary>
    /// 博文管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    //[AllowAnonymous]
    public class ArticleController : ControllerBase
    {
        readonly ILogger<ArticleController> logger;
        readonly IArticleService articleService;
        public ArticleController(ILogger<ArticleController> _logger, IArticleService _articleService)
        {
            logger = _logger;
            articleService = _articleService;
        }


        /// <summary>
        /// 查询所有博文（分页）
        /// </summary>
        /// <param name="offset">页数</param>
        /// <param name="limit">每页数</param>
        /// <returns></returns>
        [HttpPost]
        [Description("查询所有博文（分页）")]
        public async Task<MessageModel<string>> GetAllToPage(int offset=1, int limit=15)
        {
            try
            {
                return await articleService.GetAllToPage(offset,limit);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }
        }

        /// <summary>
        /// 查询所有博文（分页--前端查询）
        /// </summary>
        /// <param name="paging">入参</param>
        /// <returns></returns>
        [HttpPost]
        [Description("查询所有博文（分页--前端查询）")]
        public async Task<MessageModel<string>> GetAllToMoer([FromBody] Paging paging)
        {
            try
            {
                return await articleService.GetAllToMoer(paging);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }
        }

        /// <summary>
        /// 模糊查询
        /// </summary>
        /// <param name="blurCon">入参</param>
        /// <returns></returns>
        [HttpPost]
        [Description("模糊查询")]
        public async Task<MessageModel<List<BArticle>>> GetFuzzyQuery(string blurCon)
        {
            try
            {
                return await articleService.GetFuzzyQuery(blurCon);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<List<BArticle>>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }
        }

        /// <summary>
        /// 查询所有博文
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("查询所有博文")]
        public async Task<MessageModel<List<BArticle>>> GetAll()
        {
            try
            {
                return await articleService.GetAll();
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<List<BArticle>>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 按ID查询博文--前端
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("按ID查询博文")]
        public async Task<MessageModel<string>> GetByIdToPage(int id)
        {
            try
            {
                return await articleService.GetByIdToPage(id);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 按ID查询博文
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("按ID查询博文")]
        public async Task<MessageModel<BArticle>> GetById(int id)
        {
            try
            {
                return await articleService.GetById(id);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<BArticle>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }



        /// <summary>
        /// 按类别ID查询博文
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("按类别ID查询博文")]
        public async Task<MessageModel<List<BArticle>>> GetBySortId(int SortID)
        {
            try
            {
                return await articleService.GetByExpression<BArticle>(n=>n.SortID==SortID);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<List<BArticle>>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }



        /// <summary>
        /// 添加博文
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("添加博文")]
        public async Task<MessageModel<string>> Post([FromBody] BArticle parameter)
        {
            try
            {
                return await articleService.Post(parameter);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 修改博文
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPut]
        [Description("修改博文")]
        public async Task<MessageModel<string>> Put([FromBody]BArticle parameter)
        {
            try
            {
                return await articleService.Put(parameter);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 删除博文
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        [Description("删除博文")]
        public async Task<MessageModel<bool>> DelById(int id)
        {
            try
            {
                return await articleService.DelById(id);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<bool>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }


    }
}
